/*
 * Copyright (c) 2009-2010, Petr Panteleyev <petr@panteleyev.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.flickr;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.Box;
import javax.swing.JMenu;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.panteleyev.blogclient.BloggerFrame;
import org.panteleyev.blogclient.Icon;
import org.panteleyev.blogclient.MainClass;
import org.panteleyev.blogclient.Options;
import org.panteleyev.flickrapi.FlickrAccount;
import org.panteleyev.flickrapi.FlickrPhotoSet;
import org.panteleyev.swing.MenuProvider;
import org.panteleyev.swing.SwingFactory;
import org.panteleyev.swing.ToolBarProvider;
import org.panteleyev.swing.events.CloseFrameEvent;
import org.panteleyev.utilities.Updatable;

/**
 *
 * @author Petr Panteleyev <petr@panteleyev.org>
 */
public class PhotoSetListFrame extends BloggerFrame<FLUserProfile> implements Updatable {
    private JTabbedPane         tabs;
    private FlickrAccount       account;
    private JToolBar            panelToolBar;

    /** Creates new form PhotoSetListFrame */
    public PhotoSetListFrame(FLUserProfile profile) {
        super(profile);

        account = profile.getAccount();

        initComponents();
    }

    private void initComponents() {
        setTitle("Flickr Photos");
        setIconImage(Icon.FLICKR.I().getImage());

        getContentPane().setLayout(new BorderLayout());

        tabs = new JTabbedPane();
        PhotoSetListPanel listPanel = new PhotoSetListPanel(this, getProfile());
        tabs.add("Photo Sets", listPanel);

        getContentPane().add(
            SwingFactory.createToolBarPanel(
                panelToolBar = SwingFactory.createToolBar(false, true,
                    listPanel.getToolBarActions()
                ),
                Box.createHorizontalGlue(),
                SwingFactory.createToolBar(false, true, getUpdateAction()),
                Box.createHorizontalStrut(5),
                getBusyLabel(),
                Box.createHorizontalStrut(5)
            ), BorderLayout.NORTH
        );

        getContentPane().add(tabs, BorderLayout.CENTER);

        tabs.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent evt) {
                tabbedPaneStateChanged(evt);
            }
        });

        createMenuBar(
            createFileMenu(),
            listPanel.getMenu(),
            createToolsMenu(),
            createWindowMenu(),
            createHelpMenu()
        );

        Options.getBounds(this);
    }

    public FlickrAccount getAccount() {
        return account;
    }

    public void onOpenPhotoset(FlickrPhotoSet photoSet) {
        // check if this set is already open
        for (Component comp : tabs.getComponents()) {
            if (comp instanceof FlickrSetPanel) {
                if (((FlickrSetPanel)comp).getPhotoSet() == photoSet) {
                    tabs.setSelectedComponent(comp);
                    return;
                }
            }
        }

        FlickrSetPanel panel = new FlickrSetPanel(this, getProfile(), photoSet);
        tabs.add(photoSet.getTitle(), panel);
        tabs.setSelectedComponent(panel);
    }

    @Override
    protected boolean canClose() {
        int count = getProfile().getFramesCount(this.getClass());
        if (count == 1) {
            if (MainClass.showConfirmDialog(this)) {
                // Close all profile related frames
                getProfile().sendApplicationEvent(new CloseFrameEvent(this));
                getProfile().setConnected(false);
                return true;
            }
            return false;
        }

        return super.canClose();
    }

    @Override
    public void update() {
        Component comp = tabs.getSelectedComponent();
        if (comp instanceof Updatable) {
            ((Updatable)comp).update();
        }
    }

    private void tabbedPaneStateChanged(ChangeEvent evt) {
        Component comp = tabs.getSelectedComponent();

        if (comp instanceof MenuProvider) {
            Component c = getJMenuBar().getComponent(1);
            c.setVisible(false);
            JMenu menu = ((MenuProvider)comp).getMenu();
            menu.setVisible(true);
            getJMenuBar().remove(1);
            getJMenuBar().add(menu, 1);
        }

        if (comp instanceof ToolBarProvider) {
            panelToolBar.removeAll();
            SwingFactory.fill(panelToolBar, ((ToolBarProvider)comp).getToolBarActions());
        }
    }
}
